<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : payment.class.php   FILE_PATH : E:\www\\trunk\classes\application\payment.class.php
 * ....支付接口
 *
 * @copyright Copyright (c) 2007 - 2007 www.shopnc.net 
 * @author workflow Develop Team
 * @package 
 * @subpackage 
 * @version Mon May 05 11:12:19 CST 2008
 */

class PaymentClass extends FrameWork {
	/**
	 * 支付方式列表，并筛选出是否安装的部分
	 * @author workflow Develop Team    
	 * @param $param_array 配置文件中的支付方式参数
	 * @param $path 支付接口(payment)文件路径
	 * @return array
	 */
	function paymentList($param_array,$path){
		/*读取支付文件夹的文件目录*/
		if (is_dir($path)){
			$dir_array = array();
			/*读取文件*/
			$dh = @opendir($path);
			$i=0;
			while (($dir = @readdir($dh)) !== false){
				if ($dir != '.' && $dir != '..'){
					$dir_array[$i]['dir_name'] = $dir;
					$dir_array[$i]['is_module'] = false;
					/*判断该支付模块内的模块配置文件是否存在*/
					$dh_module = @opendir($path.$dir);
					while (($file = @readdir($dh_module)) !== false){
						if ($file != '.' && $file != '..'){
							if ($file == 'payment_module.php'){
								/*取模块配置信息*/
								include_once ($path.$dir."/payment_module.php");
								$classname = $dir."PaymentMethod";
								$obj_module = new $classname;
								$array = $obj_module->payment_param();
								$array['name'] = $array['name'];
								$dir_array[$i]['module_array'] = $array;
								unset($classname,$obj_module,$array);
								$dir_array[$i]['is_module'] = true;
								/*判断是否安装，如果安装了判断状态*/
								if ($param_array[$dir] == ""){/*已安装*/
									$dir_array[$i]['is_setup'] = false;
								}else {/*未安装*/
									$dir_array[$i]['is_setup'] = true;
									if ($param_array[$dir] == '1'){/*状态是否开启*/
										$dir_array[$i]['is_open'] = true;
									}else {
										$dir_array[$i]['is_open'] = false;
									}
								}
							}
						}
					}
					$i++;
				}
			}
			return $dir_array;
		}else {
			$report = "This dir(".$path.") isn't exists";
			Common::outMessage("json",$report,0);
		}
	}
	
	/**
	 * 调用指定的支付的接口页面
	 * @author workflow Develop Team    
	 * @param  $dir_name 支付模块的标识名称，同时也是模块路径的名称
	 * @param  $sp_code 订单号
	 * @param  $type 当前会员对于该订单的角色 1 卖家，2 买家
	 * @param  $receive_code 收货地址编码
	 * @param  $order_state 订单状态
	 * 
	 * @return array
	 */
	function returnPaymentPath($dir_name,$sp_code,$type,$receive_code,$order_state){
		//判断会员类型是否为空
		if ($type == ''){
			return false;
		}
		//判断模块文件是否存在
		if (!file_exists("../payment/".$dir_name."/index.php") || !file_exists(("../payment/".$dir_name."/payment_module.php"))){
			echo "(../payment/".$dir_name."/index.php or payment_module.php)"." is not exists";
			exit;
		}
		//取支付方式
		require_once("../payment/".$dir_name."/payment_module.php");
		$class = $dir_name.'PaymentMethod';
		$obj_payment = new $class;
		//取支付方式配置参数
		$param_array = $obj_payment->payment_param();
		//取操作url
		$value_array = array();
		$value_array['dir_name'] = $dir_name;
		$value_array['sp_code'] = $sp_code;
		$value_array['receive_code'] = $receive_code;
		$value_array['order_state'] = $order_state;
		$path = $obj_payment->payment_url($value_array);
		return $path;
	}
	
	/**
	 * 向会员扩展表中添加支付模块对应字段
	 * @author workflow Develop Team    
	 * @param  $field 数据库字段 ,$comment 字段备注
	 * @return bool
	 */
	function addPaymentField($field,$comment){
		if (trim($field) != ''){
			/*数据表字段*/
		  	$sql = "SELECT * FROM `" . NC_DBTABLEPRE . "member_extend`";
		  	$sql_rs = $GLOBALS['db']->Execute($sql);
			$field_array = $GLOBALS['db']->FieldCount($sql_rs);
			$find = false;
			for( $i = 0; $i < $field_array; $i++ ){
				/*取字段名*/
				$line = $GLOBALS['db']->FetchField($sql_rs,$i);
				if ($line == $field){
					$find = true;
				}
			}
			if (!$find){
				$sql = "ALTER TABLE `".NC_DBTABLEPRE."member_extend` ADD ".$field." VARCHAR( 100 ) NULL;";
				$query = $GLOBALS['db']->Execute($sql);
				if (!$query){
					$report = "Create field is faild ,please check you database";
					Common::outMessage("json",$report,0);
				}else {
					return true;
				}
			}else {
				return true;
			}
		}else {
			return false;
		}
	}
	
	/**
	 * 取缴费类别对应的最后一条信息
	 * @author workflow Develop Team     
	 * @param  $ps_type缴费类别
	 * @return array
	 */
	function getPaymentSwitchLast($condition){
		$substr = $this->_conditionSearch($condition);
		$array = $GLOBALS['db']->GetList($substr,$page,'payment_switch_log','*',0,'ps_log_id desc');
		return $array[0];
	}
	
	/**
	 * 构造检索条件
	 * @author workflow Develop Team     
	 * @param  $condition 检索条件数组
	 * @return string
	 */
	function _conditionSearch($condition){
		$condition_str = '';
		
		//缴费类别
		if ($condition['ps_type'] != ''){
			$condition_str .= " and ps_type = '" . $condition['ps_type'] . "'";
		}
		return $condition_str;
	}
	
	/**
	 * 增加 缴费开关操作记录
	 * @author workflow Develop Team     
	 * @param  $value_array 参数
	 * @return bool
	 */
	function addPaymentSwitch($value_array){
		if (is_array($value_array)){
			$valueArray = array();
			foreach ($value_array as $k => $v){
				if ($v != ''){
					$valueArray[$k] = $v;
				}
			}
			$result = $GLOBALS['db']->InsertRow($valueArray,"payment_switch_log","ps_log_id");
			unset($value_array,$valueArray);
			return $result;
		}
		return false;
	}
}
?>